package xrz.com.jr;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

import myhttps.Http;
import myhttps.HttpConst;
import myhttps.HttpListener;
import myhttps.HttpUtil;
import xrz.com.jr.entity.PersionEntry;
import xrz.com.jr.utils.BaseDialog;
import xrz.com.jr.utils.ExitUtil;
import xrz.com.jr.utils.PhoneUtil;
import xrz.com.jr.utils.SharedPreferencesUtils;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button Btn_Login;
    private EditText Edit_UserName;
    private EditText Edit_Password;
    private String Str_UserName = "", Str_PassWord = "";
    private TextView Tv_DeviceId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        ExitUtil.getInstant().addToPool(this);
    }

    private void init() {
        initView();
    }


    private void initView() {
        Btn_Login = (Button) findViewById(R.id.btn_login);
        Edit_Password = (EditText) findViewById(R.id.edit_password);
        Edit_UserName = (EditText) findViewById(R.id.edit_username);
        Tv_DeviceId= (TextView) findViewById(R.id.deviceid);
        Btn_Login.setOnClickListener(this);
        Tv_DeviceId.setText(PhoneUtil.GetDeviceId(this));
        checkenable();
    }

    private void checkenable() {
        Http.getInstant().sendRequest(this, true, HttpConst.usetime, null, new HttpListener() {
            @Override
            public void onHttpSuccess(String result) {
                try{
                    boolean flag = Boolean.parseBoolean(result);
                    if(!flag){
                       new AlertDialog.Builder(LoginActivity.this).setTitle("提示").setMessage("试用期已到").setNegativeButton("确定", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                               finish();
                           }
                       }).create().show();
                    }
                }catch(Exception e){}
            }

            @Override
            public void onHttpError(String error) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        Str_PassWord = Edit_Password.getText().toString();
        Str_UserName = Edit_UserName.getText().toString();
        if (Str_PassWord == null || Str_PassWord.equals("") || Str_UserName == null || Str_UserName.equals("")) {
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            String Device_ID = PhoneUtil.GetDeviceId(this);
           //  Device_ID="15838363479";
            Map<String, String> map = HttpUtil.getParams("deviceid", Device_ID, "username", Str_UserName, "password", Str_PassWord);
            Http.getInstant().sendRequest(this, true, HttpConst.getLogin, map, new HttpListener() {
                @Override
                public void onHttpSuccess(String result) {
                    JSONObject object = JSON.parseObject(result);
                    int flag = object.getInteger("success");
                    if (flag == 1) {
                        PersionEntry entry = JSON.parseObject(result, PersionEntry.class);
                        SharedPreferencesUtils.saveObject(LoginActivity.this,"persion",entry);
                        startActivity(new Intent(LoginActivity.this, TabMainActivity.class));
                        finish();
                    }else{
                        Toast.makeText(LoginActivity.this, object.get("error").toString(), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onHttpError(String error) {
                    Toast.makeText(LoginActivity.this, error, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
